#define __QYQ_ARITHMETIC_RECEIVE_C_
#include "qyq_arithmetic_receive.h"

int8_t qyq_arithmetic_receive(uint8_t new_value, uint16_t new_tick, uint16_t *rx_buf, uint16_t *rx_length, qyq_arithmetic_receive_t *qyq_arithmetic_config)
{
    if ((rx_buf == null) || (rx_length == null))
    {
        return -1;
    }

    // 超时机制和buf接收满机制判断
    if(((new_tick - qyq_arithmetic_config->qyq_arithmetic_receive_tick) > qyq_arithmetic_config->qyq_arithmetic_receive_timeout)||(qyq_arithmetic_config->qyq_arithmetic_receive_index >= qyq_arithmetic_config->qyq_arithmetic_receive_max_size))
    {
        *rx_length = qyq_arithmetic_config->qyq_arithmetic_receive_index;
        qyq_arithmetic_config->qyq_arithmetic_receive_count = 0;
        qyq_arithmetic_config->qyq_arithmetic_receive_index = 0;

        return -2;
    }

    // 更新时间和数据
    qyq_arithmetic_config->qyq_arithmetic_receive_tick = new_tick;
    new_tick[qyq_arithmetic_config->qyq_arithmetic_receive_index ++] = new_value;

    // 判断是否为结束标志
    if(qyq_arithmetic_config->qyq_arithmetic_receive_end_list[qyq_arithmetic_config->qyq_arithmetic_receive_count] == new_value)
    {
        qyq_arithmetic_config->qyq_arithmetic_receive_count ++;
    }
    else
    {
        qyq_arithmetic_config->qyq_arithmetic_receive_count = 0;
    }

    // 检测结束标志是否判断完成
    if(qyq_arithmetic_config->qyq_arithmetic_receive_end_size == qyq_arithmetic_config->qyq_arithmetic_receive_count)
    {
        *rx_length = qyq_arithmetic_config->qyq_arithmetic_receive_index;
        qyq_arithmetic_config->qyq_arithmetic_receive_count = 0;
        qyq_arithmetic_config->qyq_arithmetic_receive_index = 0;
        return 1;
    }

    return 0;
}
